<?php
namespace jy_merch;
defined('BY_JYA') or exit('error');
/*
	本插件：$this->model('goods')->方法名();
	其他地方调用：model('jy_merch/goods')->方法名();
*/
class goods extends \plugin{
	/*
		商品列表
	*/
	public function uplist($list=array()){
		foreach($list as &$itemv){
			$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
			$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
			$itemv['cartid']=$cartinfo['id']?:0;
			$itemv['carttotal']=$cartinfo['total']?:0;
			$itemv['cartprice']=$cartinfo['price']?:0;
			$itemv['cartoptionid']=$cartinfo['optionid']?:0;
			$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
			$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
			$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
			//规格
			$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$itemv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
			//已选规格
			if($itemv['options']['id'] && $itemv['options']['specs']){
				$spsids=$itemv['options']['specs'];
				$selectspecs=explode("_",$itemv['options']['specs']);
				$selespename = '';
				if($selectspecs){
					foreach($selectspecs as $k=>$spid){
						$spec_item=pdo_get('jy_weishop_goods_spec_item',array('id'=>$spid,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
						$selespename .=$spec_item['title'].'，';
					}
				}
				if($specs_list){
					foreach($specs_list as $k=>&$sp){
						$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
					}
				}
			}else{
				$selectspecs = array();
				$selespename = '';
				if($specs_list){
					foreach($specs_list as $k=>&$sp){
						$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
						$selectspecs[$k]=$sp['items'][0]['id'];
						$selespename .=$sp['items'][0]['title'].'，';
					}
				}
				//默认已选参数：
				$spsids = '';
				foreach($selectspecs as $k=>&$s){
					if($k==0){
						$spsids .= $s;
					}else{
						$spsids .= '_'.$s;
					}
				}
			}
			if($_SESSION['store_id']){
				$getinfo=model('jy_weishop/store')->getinfo($itemv['id'],$_SESSION['store_id']);
				$itemv['total']=$getinfo['id']?$getinfo['total']:$itemv['total'];
				$itemv['price']=$getinfo['id']?$getinfo['price']:$itemv['price'];
				$itemv['options']=model('jy_weishop/store')->getoption($itemv['id'],$_SESSION['store_id'],$spsids);
			}
			if(!$itemv['options']['id']){
				$itemv['options'] = pdo_get('jy_weishop_goods_option',array('goodsid'=>$itemv['id'],'specs'=>$spsids));
			}
			//默认商品规格价格：
			if($itemv['options']){
					$itemv['price']=$itemv['options']['price'];
					if($itemv['active']['discount']>10){
						$itemv['options']['oldprice']=$itemv['options']['price'];
						$itemv['price']=$itemv['options']['oldprice']*$itemv['active']['discount']/100;
						$itemv['options']['price']=$itemv['price'];
					}
					$itemv['post'][$itemv['options']['id']]=array('num'=>0,'title'=>$selespename);
					$op_carts=pdo_getall('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),array('id','optionid','total'));
					foreach($op_carts as $ov){
						$specs = pdo_getcolumn('jy_weishop_goods_option',array('id'=>$ov['optionid']),'specs');
						$specs=explode("_",$specs);
						foreach($specs as &$spec){
							$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
						}
						$optitle=implode("，",$specs);
						$itemv['post'][$ov['optionid']]=array('num'=>$ov['total']?:0,'title'=>$optitle);
					}
			}
			$itemv['selespename'] = $selespename;
			$itemv['specs_list'] = $specs_list;
			$itemv['specid']=$selectspecs;
			$itemv['optionid']=$itemv['options']['id'];
			$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
			$itemv['red_fee']=$this->model('model')->red_fee($itemv['price']);
			if(!$itemv['pcate2']){
				$itemv['pcate2']=pdo_getcolumn('jy_weishop_goods',array('id'=>$itemv['id'], 'uniacid' => $_SESSION['uniacid']),'pcate2');
				if(!$itemv['pcate2']){
					$itemv['pcate2']=$itemv['pcate'];
					pdo_update('jy_weishop_goods',array('pcate2'=>$itemv['pcate'],'ccate2'=>$itemv['ccate'],'scate2'=>$itemv['scate']),array('id'=>$itemv['id'], 'uniacid' => $_SESSION['uniacid']));
				}
			}
		}
        return $list;
    }
	/*
		分类+商品列表
	*/
	public function upcategorygoods($category=array(),$merchid=0,$rqdata=array()){
		//商品开始
		$params=array('uniacid'=>$_SESSION['uniacid'],'type <>'=>4,'status'=>array(0,1),'nochecked'=>0,'merchid'=>$merchid);
		if(trim($rqdata['keyword'])){
			$params['title like']='%'.trim($rqdata['keyword']).'%';
			$return['params']['keyword']=trim($rqdata['keyword']);
		}
		if($rqdata['keyword']){		
			$params['title like']='%'.trim($rqdata['keyword']).'%';
			$return['params']['title']=$rqdata['keyword'];	
		}
		$fields=array('id','status','title','subtitle','image','video','price','active_plugin','active_id','salenum','pluginname','originalprice','total','weight','unit');
		$orderby='paixu desc ,salenum desc , id desc ';
		if($rqdata['order']){
			if($rqdata['order']=='sales' || $rqdata['order']==0){
				$orderby='salerealnum desc';
			}
			if($rqdata['order']==1){
				$params['saletype likes']=array('1');
			}
			if($rqdata['order']==2){
				$params['saletype likes']=array('3');
			}
			if($rqdata['order']=='price'){
				if($rqdata['order_type']==2){
					$orderby='price desc';
				}else{
					$orderby='price asc';
				}
			}
		}
		$pindex = max(1, 1);
		$psize = 100;
		foreach($category as $k=>&$v){
			$params['pcate2']=$v['id'];
			$parameter=array(
				'params'=>$params,
				'limit'=>array($pindex,$psize),
				'fields'=>$fields,
				'orderby'=>$orderby,
			);
			$goodslist=model('jy_weishop/goods')->goods_list($parameter,$total);
			if(empty($goodslist)){
			    $parameter['params']['pcate']=$v['id'];
				$goodslist=model('jy_weishop/goods')->goods_list($parameter,$total);
			}
			if(empty($goodslist)){
				unset($category[$k]);
				continue;
			}
			foreach($goodslist as &$itemv){
				$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
				$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
				$itemv['cartid']=$cartinfo['id']?:0;
				$itemv['carttotal']=$cartinfo['total']?:0;
				$itemv['cartprice']=$cartinfo['price']?:0;
				$itemv['cartoptionid']=$cartinfo['optionid']?:0;
				$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
				$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
				$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
				//规格
					$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$itemv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
					//已选规格
					$selectspecs = array();
					$selespename = '';
					if($specs_list){
							foreach($specs_list as $k=>&$sp){
								$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
								$selectspecs[$k]=$sp['items'][0]['id'];
								$selespename .=$sp['items'][0]['title'].'，';
							}
					}
					//默认已选参数：
					$spsids = '';
					foreach($selectspecs as $k=>&$s){
							if($k==0){
								$spsids .= $s;
							}else{
								$spsids .= '_'.$s;
							}
					}
					if($_SESSION['store_id']){
						$getinfo=model('jy_weishop/store')->getinfo($itemv['id'],$_SESSION['store_id']);
						$itemv['total']=$getinfo['id']?$getinfo['total']:$itemv['total'];
						$itemv['price']=$getinfo['id']?$getinfo['price']:$itemv['price'];
						$itemv['options']=model('jy_weishop/store')->getoption($itemv['id'],$_SESSION['store_id'],$spsids);
					}
					if(!$itemv['options']['id']){
						$itemv['options'] = pdo_get('jy_weishop_goods_option',array('goodsid'=>$itemv['id'],'specs'=>$spsids));
					}
					//默认商品规格价格：
					if($itemv['options']){
						$itemv['price']=$itemv['options']['price'];
						if($itemv['active']['discount']>10){
							$itemv['options']['oldprice']=$itemv['options']['price'];
							$itemv['price']=$itemv['options']['oldprice']*$itemv['active']['discount']/100;
							$itemv['options']['price']=$itemv['price'];
						}
						$itemv['post'][$itemv['options']['id']]=array('num'=>0,'title'=>$selespename);
						$op_carts=pdo_getall('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),array('id','optionid','total'));
						foreach($op_carts as $ov){
							$specs = pdo_getcolumn('jy_weishop_goods_option',array('id'=>$ov['optionid']),'specs');
							$specs=explode("_",$specs);
							foreach($specs as &$spec){
								$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
							}
							$optitle=implode("，",$specs);
							$itemv['post'][$ov['optionid']]=array('num'=>$ov['total']?:0,'title'=>$optitle);
						}
					}
					$itemv['selespename'] = $selespename;
					$itemv['specs_list'] = $specs_list;
					$itemv['specid']=$selectspecs;
					$itemv['optionid']=$itemv['options']['id'];
					$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
					$itemv['red_fee']=$this->model('model')->red_fee($itemv['price']);
			}
			$v['list']=$goodslist;
			$v['total']=0;
			foreach($goodslist as $ag){
				$carttotal=pdo_getsum('jy_weishop_member_cart',array('goodsid'=>$ag['id'],'selected'=>1,'mid'=> $_SESSION['uid'],'uniacid' => $_SESSION['uniacid']),'total');
				$v['total'] +=$carttotal;
			}
		}
		return $category;
	}
}